﻿
using LightCAD.MathLib;
namespace LightCAD.Core.Elements
{
    public class LcEllipse : LcCurve2d
    {
        public Vector2 Center { get => this.Ellipse.Center; set => this.Ellipse.Center = value; }
        public Vector2 AxisX
        {
            [MethodImpl((MethodImplOptions)768)]
            get;
           
        }
            = Vector2.UnitX;
        public double Rotation
        {
            get => this.Ellipse.Rotation; 
            set
            {
                this.Ellipse.Rotation = value;
                this.AxisX.Set(1,0).RotateAround(0, 0, value);
            }
        }
        public double RadiusX{ get => this.Ellipse.RadiusX; set => this.Ellipse.RadiusX = value; }
        public double RadiusY { get => this.Ellipse.RadiusY; set => this.Ellipse.RadiusY = value; }
        public double StartAngle { get => this.Ellipse.StartAngle; set => this.Ellipse.StartAngle = value; }
        public double EndAngle { get => this.Ellipse.EndAngle; set => this.Ellipse.EndAngle = value; }
        public bool IsClockwise { get => this.Ellipse.IsClockwise; set => this.Ellipse.IsClockwise = value; }
        public Ellipse2d Ellipse=>this.Curve as Ellipse2d;
        public LcEllipse()
        {
            this.Curve=new Ellipse2d();
            this.StartAngle = 0;
            this.EndAngle = Utils.TwoPI;
            this.Type=BuiltinElementType.Ellipse;
        }
        public LcEllipse(Vector2 center, double radiusX, double radiusY,double rotation, double startAngle = 0, double endAngle = Utils.TwoPI):this()
        {
            this.Center = center;
            this.Rotation = rotation;
            this.RadiusX = radiusX;
            this.RadiusY = radiusY;
            this.StartAngle = startAngle;
            this.EndAngle = endAngle;
            this.IsClockwise = false;
        }


        public override LcElement Clone()
        {
            var clone = new LcEllipse();
            clone.Copy(this);
            return clone;
        }

        public override void WriteProperties(Utf8JsonWriter writer, JsonSerializerOptions soptions)
        {
            base.WriteProperties(writer, soptions);
            writer.WriteVector2dProperty(nameof(this.Center), this.Center);
            writer.WriteVector2dProperty(nameof(this.AxisX), this.AxisX);
            writer.WriteNumberProperty(nameof(this.RadiusX), this.RadiusX);
            writer.WriteNumberProperty(nameof(this.RadiusY), this.RadiusY);
            writer.WriteNumberProperty(nameof(this.StartAngle), this.StartAngle);
            writer.WriteNumberProperty(nameof(this.EndAngle), this.EndAngle);
        }


        public override void ReadProperties(ref JsonElement jele)
        {
            base.ReadProperties(ref jele);
            this.Center = jele.ReadVector2dProperty(nameof(this.Center));
            this.Rotation = jele.ReadDoubleProperty(nameof(this.Rotation));
            this.RadiusX = jele.ReadDoubleProperty(nameof(this.RadiusX));
            this.RadiusY = jele.ReadDoubleProperty(nameof(this.RadiusY));
            this.StartAngle = jele.ReadDoubleProperty(nameof(this.StartAngle));
            this.EndAngle = jele.ReadDoubleProperty(nameof(this.EndAngle));
        }
    }
}
